<?php
require_once dirname(__FILE__).'/../Plugin.php';

class Plugin_AccessUsers_Class extends My_Plugin
{
    function  __construct($wertex)
    {
        parent::__construct('access_users', $wertex);

        $this->accessTypes = array(
            'guest'     => 'Viesiem',
            'student'   => 'Studentiem',
            'teacher'   => 'Skolotājiem'
        );
        $this->_initForm();

    }

    function action()
    {
        $post = $this->request->getPost();
        if ($this->request->isPost() AND $post[$this->getPrefix().'submit']) {
            if($this->add()){
                return true;
            }
        }
    }

    function getContent()
    {
        $access = $this->getAccess();
        if(is_array($access)){
            $accessStr = implode(", ", $access);
        }

        return $this->toTemplate(array('CONTENT' => $accessStr), 'index');
    }

    function getHeader(){
        $access = $this->getAccess();
        if(is_array($access)){
            $accessStr = implode(", ", $access);
        }
        return $accessStr;
    }

    function getAccess(){
        $row = $this->getLastRow();
        return unserialize($row['access']);
    }

    function add()
    {
        $values = $this->request->getPost();
        if ($this->form->isValid($values)) {

            $insertData = array();
            foreach ($this->getCols() AS $column) {
                if($column == 'access'){
                    $insertData[$column] = serialize($values[$this->getPrefix().$column]);
                }else{
                    $insertData[$column] = $values[$this->getPrefix().$column];
                }
            }
            if($this->insert($insertData)){
                $this->setMessage('Veiksmīgi saglabāts');
                return $this->getLastId();
            }
        } else {
            $this->form->populate($values);
        }
    }

    function body()
    {
        $content = file_get_contents(dirname(__FILE__).'/template/form.html');
        $content = str_replace('{FORM}', $this->form, $content);
        return $content;
    }

    function _initForm()
    {
        $this->form = new Zend_Form;
        $this->form->addElement('hidden', $this->getPrefix().'wertex');
        $this->form->addElement('hidden', $this->getPrefix().'date');


        $this->form->addElement('multiselect', $this->getPrefix().'access',
            array(
                'label'     => 'Atļaut:',
                'filters'   => array('StringTrim', 'StripTags'),
                'required'  => false,
                'MultiOptions' => $this->accessTypes,
                'value'   => $this->getAccess(),
                'class' => 'multiselect',
                'multiple' => 'multiple',
            )
        );

        $this->form->addElement('submit', $this->getPrefix().'submit',
            array(
                'ignore'   => true,
                'label'    => 'Saglabāt',
            )
        );

        $lastRow = $this->getLastRow();
        $this->form->setDefault($this->getPrefix().'name', $lastRow['name']);

        $this->form->setDefault($this->getPrefix().'wertex', $this->getWertex());
        $this->form->setDefault($this->getPrefix().'date', date("Y-m-d H:i:s"));
    }

    public function userAccessAllowed($type){
        return in_array($type, $this->getAccess());
    }

    public function isVisual(){
        return false;
    }

}
